package com.zlk.algorithm.algorithm.dynamicPlan.twoDimension;


import com.zlk.algorithm.algorithm.sort.heap.Test;

// 单词搜索（无法改成动态规划）
// 给定一个 m x n 二维字符网格 board 和一个字符串单词 word
// 如果 word 存在于网格中，返回 true ；否则，返回 false 。
// 单词必须按照字母顺序，通过相邻的单元格内的字母构成
// 其中"相邻"单元格是那些水平相邻或垂直相邻的单元格
// 同一个单元格内的字母不允许被重复使用
// 测试链接 : https://leetcode.cn/problems/word-search/
public class Code02_WordSearch {


    public static void main(String[] args) {
        char[][] grid  =new char[3][4];
        grid[0] = new char[]{'A','B','C','E'};
        grid[1] = new char[]{'S','F','C','S'};
        grid[2] = new char[]{'A','D','E','E'};
        int row = grid.length;
        int col = grid[0].length;
        System.out.println(row);
        System.out.println(col);
        Code02_WordSearch search = new Code02_WordSearch();
        System.out.println(search.exist(grid, "ABCCED"));
    }


    public  boolean exist(char[][] board, String word) {
        int row = board.length;
        int col = board[0].length;
        char[] chars = word.toCharArray();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if(f(board,i,j,chars,0)){
                    return true;
                }
            }
        }
        return false;
    }

    // 因为board会改其中的字符
    // 用来标记哪些字符无法再用
    // 带路径的递归无法改成动态规划或者说没必要
    // 从(i,j)出发，来到w[k]，请问后续能不能把word走出来w[k...]
    public static boolean f(char[][] b, int i, int j, char[] w, int k) {
        if (k == w.length) {
            return true;
        }
        if (i < 0 || i == b.length || j < 0 || j == b[0].length || b[i][j] != w[k]) {
            return false;
        }
        // 不越界，b[i][j] == w[k]
        char tmp = b[i][j];
        b[i][j] = 0;
        boolean ans = f(b, i - 1, j, w, k + 1)
                || f(b, i + 1, j, w, k + 1)
                || f(b, i, j - 1, w, k + 1)
                || f(b, i, j + 1, w, k + 1);
        b[i][j] = tmp;
        return ans;
    }

    private boolean f1MySelf(char[][] board, int i, int j, char[] chars, int index) {
        if(board[i][j]==chars[index++]){
            if(index-1==chars.length-1){
                return true;
            }else{
                int row = board.length;
                int col = board[0].length;
                char temp = board[i][j];
                board[i][j] = '/';
                boolean res1=false, res2=false,res3=false,res4=false;
                //if(i+1<=row-1&&board[i+1][j]!='/'){
                if(i+1<=row-1){
                    res1 = f1MySelf(board,i+1,j,chars,index);
                }
                //if(i-1>=0&&board[i-1][j]!='/'){
                if(i-1>=0){
                    res2 = f1MySelf(board,i-1,j,chars,index);
                }
                //if(j+1<=col-1&&board[i][j+1]!='/'){
                if(j+1<=col-1){
                    res3 = f1MySelf(board,i,j+1,chars,index);
                }
                //if(j-1>=0&&board[i][j-1]!='/'){
                if(j-1>=0){
                    res4 = f1MySelf(board,i,j-1,chars,index);
                }
                board[i][j] = temp;
                return res1|res2|res3|res4;
            }
        }else{
            return false;
        }
    }


}
